'use strict';
// 通过 require 引入 vk 实例
const vk = require('vk-unicloud');
// 通过 vk.init 初始化 vk实例（只有初始化后才能使用）
vk.init({
	baseDir: __dirname,
	requireFn: require
});
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/pages/files_mzgl/dy_tv/kh/videoAudit 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			ma_app_id,
			album_id
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		
		let A = await vk.baseDao.selects({
		  dbName:"a_appid",
		  // 主表where条件
		  whereJson:{
		    appid:ma_app_id
		  },
		});
		
		console.log("A========",A);
		
		
		let appsecret = A.rows[0].appsecret
		
		//获取accessToken
		let urlA = "https://open.douyin.com/oauth/client_token/"
		const accessToken = await uniCloud.httpclient.request(urlA, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
			},
			data: {
				client_key: ma_app_id, // id
				client_secret: appsecret, // 密钥
				grant_type: "client_credential",
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})


		// 有效时间为 2 个小时
		let access_token = accessToken.data.data.access_token

		// 短剧送审
		let urlT = "https://open.douyin.com/api/playlet/v2/video/review/"
		let newinfo= await uniCloud.httpclient.request(urlT, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
				"access-token": access_token,
			},
			data: {
				ma_app_id: ma_app_id,
				album_id: album_id,
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})
		console.log("newinfo====",newinfo);

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}